import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../model/model.dart';
import '../../model/model_default.dart';
import 'effect_view.dart';

class ChooseGroup extends BaseDialog {
  GroupModel group;
  void Function(GroupModel)? onChoose;
  ChooseGroup({super.key, required this.group});

  @override
  BaseDialogState createState() => _ChooseGroupState();
}

class _ChooseGroupState extends BaseDialogState<ChooseGroup> {
  late final List<GroupModel> _group_list;

  @override
  void initState() {
    super.initState();
    List<ChannelModel> ch_list = List.generate(8, ($0) {
      ChannelModel ch = initCH($0);
      return ch;
    });

    _group_list = List.generate(4, (index) {
      ChannelModel ch1 = ch_list[2 * index];
      ChannelModel ch2 = ch_list[2 * index + 1];
      return GroupModel(
          name: 'Grouping\nCH${ch1.num}&${'${ch2.num}'}', ch1: ch1, ch2: ch2);
    });
  }

  ///选择回调
  void select_function(ChannelModel ch) {
    setState(() {
      ch.select = true;
    });

    widget.group.select = true;

    ///记住优先选择的ch
    widget.group.target_ch = widget.group.ch1!.name == ch.name
        ? widget.group.ch1!
        : widget.group.ch2!;

    ///处理外面的group
    if (widget.group.contain(ch) && widget.onChoose != null) {
      widget.onChoose!(widget.group);
    }

    Future.delayed(const Duration(milliseconds: 500), () {
      Navigator.pop(context);
    });
  }

  ///
  Widget _get_row(double w, double h, GroupModel group) {
    return Row(
      children: [
        get_item(
            group.ch1!.name,
            w / 2.0 - 0.25,
            h,
            widget.group.contain(group.ch1!),
            group.ch1!.select,
            () => select_function(group.ch1!)),
        Container(width: 0.5, color: Colors.black),
        get_item(
            group.ch2!.name,
            w / 2.0 - 0.25,
            h,
            widget.group.contain(group.ch2!),
            group.ch2!.select,
            () => select_function(group.ch2!)),
      ],
    );
  }

  @override
  Widget buildContent(BuildContext context) {
    return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constrains) {
      double width = constrains.maxWidth - 20;
      return SizedBox(
        height: 210,
        child: Container(
          padding: const EdgeInsets.only(left: 10, right: 10, bottom: 10),
          child: Column(
            children: [
              const SizedBox(
                height: 39,
                child: Center(
                  child: Text(
                    'Choose what channel as a benchmark',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                        color: Colors.black),
                  ),
                ),
              ),
              Container(height: 1, color: Colors.black),
              SizedBox(
                height: 160,
                child: GridView.extent(
                  maxCrossAxisExtent: width,
                  crossAxisSpacing: 0,
                  mainAxisSpacing: 0,
                  childAspectRatio: width / 40.0,
                  physics: const NeverScrollableScrollPhysics(),
                  children: _group_list.map(($0) {
                    return _get_row(width, 40, $0);
                  }).toList(),
                ),
              )
            ],
          ),
        ),
      );
    });
  }
}
